package pres.seanlang.utility;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 *  properties工具类
 *
 * @author SeanLang
 */
final public class PropertyUtils {
    private static final String TAG = "PropertyUtils";

    protected PropertyUtils() { throw new UnsupportedOperationException("u can't instance..."); }

    /**
     *  创建properties实例
     * @param stream
     * @return
     */
    public static Properties create(InputStream stream) {
        Properties properties = new Properties();
        try {
            properties.load(stream);
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return properties;
    }

    /**
     * 创建properties实例
     * @param path
     * @return
     */
    public static Properties create(String path) {
        try {
            FileInputStream inputStream = new FileInputStream(path);
            return create(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return new Properties();
    }

    /**
     *  生成配置文件
     * @param out
     * @param properties
     * @param desc
     * @return
     */
    public static boolean store(OutputStream out, Properties properties, String desc) {
        try {
            properties.store(out, desc);
            out.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 生成配置文件
     * @param out
     * @param properties
     */
    public static void store(OutputStream out, Properties properties) {
        store(out, properties, "");
    }

    /**
     * 生成配置文件
     * @param out
     * @param data
     * @param desc
     */
    public static void store(OutputStream out, Map<String, Object> data, String desc) {
        Properties properties = new Properties();
        properties.putAll(data);
        store(out, properties, desc);
    }

    /**
     * 生成配置文件
     * @param out
     * @param data
     */
    public static void store(OutputStream out, Map<String, Object> data) {
        store(out, data, "");
    }

    /**
     * 生成配置文件
     * @param path
     * @param data
     * @param desc
     */
    public static void store(String path, Map<String, Object> data, String desc) {
        try {
            FileOutputStream out = new FileOutputStream(path);
            store(out, data, desc);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成配置文件
     * @param path
     * @param data
     */
    public static void store(String path, Map<String, Object> data) {
        store(path, data, "");
    }

    /**
     * 生成配置文件
     * @param path
     * @param properties
     * @param desc
     */
    public static void store(String path, Properties properties, String desc) {
        try {
            FileOutputStream out = new FileOutputStream(path);
            store(out, properties, desc);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成配置文件
     * @param path
     * @param properties
     */
    public static void store(String path, Properties properties) {
        store(path, properties, "");
    }

    /**
     *  读取properties配置文件中指定的属性值
     * @param stream    需要读取properties文件的stream
     * @param key       需要读取的key
     * @param def       默认值, 如果没有读取到对应的key值则返回def值
     * @return
     */
    public static String readString(InputStream stream, String key, String def) {
        Properties properties = create(stream);
        return properties.getProperty(key, def).trim();
    }

    /**
     *  读取properties配置文件指定的属性值
     * @param stream    需要读取properties文件的stream
     * @param key       需要读取的key
     * @return
     */
    public static String readString(InputStream stream, String key) {
        return readString(stream, key, "");
    }

    /**
     *  读取properties配置文件指定的属性值
     * @param stream    需要读取properties文件的stream
     * @param key       需要读取的key
     * @param def       默认值
     * @return
     */
    public static int readInt(InputStream stream, String key, int def) {
        String value = readString(stream, key, String.valueOf(def));
        return Integer.parseInt(value);
    }

    /**
     *  读取properties配置文件指定的属性值
     * @param stream    需要读取properties文件的stream
     * @param key       需要读取的key
     * @return
     */
    public static int readInt(InputStream stream, String key) {
        return readInt(stream, key, 0);
    }

    /**
     *  读取properties配置文件指定的属性值
     * @param stream    需要读取properties文件的stream
     * @param key       需要读取的key
     * @param def       默认值
     * @return
     */
    public static long readLong(InputStream stream, String key, long def) {
        String value = readString(stream, key, String.valueOf(def));
        return Long.parseLong(value);
    }

    /**
     *  读取properties配置文件指定的属性值
     * @param stream    需要读取properties文件的stream
     * @param key       需要读取的key
     * @return
     */
    public static long readLong(InputStream stream, String key) {
        return readLong(stream, key, 0);
    }

    public static long readLong(Properties properties, String key) {
        String value = properties.getProperty(key, String.valueOf(0L));
        return Long.parseLong(value);
    }

    /**
     *  读取properties配置文件指定的属性值
     * @param stream    需要读取properties文件的stream
     * @param key       需要读取的key
     * @param def       默认值
     * @return
     */
    public static double readDouble(InputStream stream, String key, double def) {
        String value = readString(stream, key, String.valueOf(def));
        return Double.parseDouble(value);
    }

    /**
     *  读取properties配置文件指定的属性值
     * @param stream    需要读取properties文件的stream
     * @param key       需要读取的key
     * @return
     */
    public static double readDouble(InputStream stream, String key) {
        return readDouble(stream, key, 0.0f);
    }

    public static double readDouble(Properties properties, String key) {
        String value = properties.getProperty(key, String.valueOf(0.0f));
        return Double.parseDouble(value);
    }

    /**
     *  读取properties配置文件中所有key-value
     * @param stream    需要读取properties文件的stream
     * @return
     */
    public static Map<String, String> readAll(InputStream stream) {
        Properties properties = create(stream);
        Map<String, String> result = new HashMap<>();
        Iterator<Object> iterator = properties.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            result.put(key, properties.getProperty(key));
        }
        return result;
    }
}
